@extends('Admin.layout.layout')
@section('content')
    <style>
        .btn-xs{
            margin:0 5px;
        }
    </style>
    <div class="wrapper wrapper-content  animated fadeInRight">
        <h2>类目管理</h2>
        <div class="row">
            <div class="col-sm-4" style="width:100%;">
                <div id="nestable-menu">
                    <button type="button" data-action="expand-all" class="btn btn-white btn-sm">展开</button>
                    <button type="button" data-action="collapse-all" class="btn btn-white btn-sm">收起</button>
                    <!-- <button class="btn btn-info " type="button"><i class="fa fa-paste"></i> 保存</button> -->
                   <!-- <a class="btn-sm btn-danger sev" data-id="1"><i class="fa fa-paste"></i>保存</a>-->
                    <a href="" class="btn btn-sm btn-success "><i class="fa fa-refresh"></i> 刷新</a>
                    <a class="btn btn-sm btn-primary fr10"  data-toggle="modal" data-target="#addModal"style="float:right;">新增</a>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6" style="width:100%;">
                <div class="ibox ">
                    <div class="ibox-content">
                        <div class="dd" id="nestable">
                            <ol class="dd-list">
                               
                                 @if(!empty($cate1))
                                 @foreach ($cate1 as $c1)
                                <li class="dd-item" data-id="{{$c1['id']}}">
                                    <div class="dd-handle">{{$c1['id']}} - {{$c1['name']}}
                                        <img width="40" src="{{$c1['pic']}}">
                                    </div>
                                    <td class="center">
                                        <a class="btn-xs btn-danger del" data-id="{{$c1['id']}}" style="float:right;position: relative;top:-34px;">删除</a>
                                        <a class="btn-xs btn-primary "  data-id="{{$c1['id']}}" data-attrs="{{$c1['attrs']}}" data-pid="{{$c1['pid']}}" data-cat1="{{$c1['first_id']}}" data-pic="{{$c1['pic']}}" data-name="{{$c1['name']}}"  data-cat-sn="{{$c1['cat_sn']}}" data-en-name="{{$c1['en_name']}}" data-sort="{{$c1['sort']}}"  data-initial="{{$c1['initial']}}"  data-toggle="modal" data-target="#editModal"style="float:right;position: relative;top:-34px;">编辑</a>
                                        @if($c1['is_index_recommend']==1)
                                            <a class="btn-xs btn-primary recommend" data-id="{{$c1['id']}}" data-name="{{$c1['name']}}"
                                               data-rec="{{$c1['is_index_recommend']}}" style="float:right;position: relative;top:-34px;">取消推荐</a>
                                        @else
                                            <a class="btn-xs btn-primary recommend" data-id="{{$c1['id']}}" data-name="{{$c1['name']}}"
                                               data-rec="{{$c1['is_index_recommend']}}" style="float:right;position: relative;top:-34px;">推荐</a>
                                        @endif
                                        
                                         @if($c1['show_status']==1)
                                            <a class="btn-xs btn-primary show_status" data-id="{{$c1['id']}}" data-name="{{$c1['name']}}"
                                               data-status="{{$c1['show_status']}}" style="float:right;position: relative;top:-34px;">隐藏</a>
                                        @else
                                            <a class="btn-xs btn-primary show_status" data-id="{{$c1['id']}}" data-name="{{$c1['name']}}"
                                               data-status="{{$c1['show_status']}}" style="float:right;position: relative;top:-34px;">显示</a>
                                        @endif

                                        @if($c1['is_shopindex_recommend']==1)
                                            <a class="btn-xs btn-primary shopindex_recommend" data-id="{{$c1['id']}}" data-name="{{$c1['name']}}"
                                               data-rec="{{$c1['is_shopindex_recommend']}}" style="float:right;position: relative;top:-34px;">取消商城推荐</a>
                                        @else
                                            <a class="btn-xs btn-primary shopindex_recommend" data-id="{{$c1['id']}}" data-name="{{$c1['name']}}"
                                               data-rec="{{$c1['is_shopindex_recommend']}}" style="float:right;position: relative;top:-34px;">商城推荐</a>
                                        @endif
                                        
                                    </td>
                                    <ol class="dd-list">
                                      @if(isset($cate2[$c1['id']]))
                                 @foreach ($cate2[$c1['id']] as $c2)
                                        <li class="dd-item" data-id="{{$c2['id']}}">
                                            <div class="dd-handle">{{$c2['id']}} - {{$c2['name']}}
                                            <img width="40" src="{{$c2['pic']}}">
                                            </div>
                                            <td class="center">
                                                <a class="btn-xs btn-danger del" data-id="{{$c2['id']}}" style="float:right;position: relative;top:-34px;">删除</a>
                                                <a class="btn-xs btn-primary "  data-id="{{$c2['id']}}"  data-attrs="{{$c2['attrs']}}"  data-pid="{{$c2['pid']}}"  data-cat1="{{$c2['first_id']}}" data-pic="{{$c2['pic']}}" data-name="{{$c2['name']}}"  data-cat-sn="{{$c2['cat_sn']}}" data-en-name="{{$c2['en_name']}}" data-sort="{{$c2['sort']}}"  data-initial="{{$c2['initial']}}"  data-toggle="modal" data-target="#editModal"style="float:right;position: relative;top:-34px;">编辑</a>
                                                @if($c2['is_index_recommend']==1)
                                                    <a class="btn-xs btn-primary recommend" data-id="{{$c2['id']}}" data-name="{{$c2['name']}}"
                                                       data-rec="{{$c2['is_index_recommend']}}" style="float:right;position: relative;top:-34px;">取消推荐</a>
                                                @else
                                                    <a class="btn-xs btn-primary recommend" data-id="{{$c2['id']}}" data-name="{{$c2['name']}}"
                                                       data-rec="{{$c2['is_index_recommend']}}" style="float:right;position: relative;top:-34px;">推荐</a>
                                                @endif
                                                  @if($c2['show_status']==1)
                                            <a class="btn-xs btn-primary show_status" data-id="{{$c2['id']}}" data-name="{{$c2['name']}}"
                                               data-status="{{$c2['show_status']}}" style="float:right;position: relative;top:-34px;">隐藏</a>
                                        @else
                                            <a class="btn-xs btn-primary show_status" data-id="{{$c2['id']}}" data-name="{{$c2['name']}}"
                                               data-status="{{$c2['show_status']}}" style="float:right;position: relative;top:-34px;">显示</a>
                                        @endif

                                            @if($c2['is_shopindex_recommend']==1)
                                                <a class="btn-xs btn-primary shopindex_recommend" data-id="{{$c2['id']}}" data-name="{{$c2['name']}}"
                                                   data-rec="{{$c2['is_shopindex_recommend']}}" style="float:right;position: relative;top:-34px;">取消商城推荐</a>
                                            @else
                                                <a class="btn-xs btn-primary shopindex_recommend" data-id="{{$c2['id']}}" data-name="{{$c2['name']}}"
                                                   data-rec="{{$c2['is_shopindex_recommend']}}" style="float:right;position: relative;top:-34px;">商城推荐</a>
                                            @endif
                                        
                                            </td>
                                            <ol class="dd-list">
                                                @if(isset($cate3[$c2['id']]))
                                				@foreach ($cate3[$c2['id']] as $c3)
                                                <li class="dd-item" data-id="{{$c3['id']}}">
                                                    <div class="dd-handle">{{$c3['id']}} - {{$c3['name']}}
                                                    <img width="40" src="{{$c3['pic']}}">
                                                    </div>
                                                    <td class="center">
                                                        <a class="btn-xs btn-danger del" data-id="{{$c3['id']}}" style="float:right;position: relative;top:-34px;">删除</a>
                                                        <a class="btn-xs btn-primary "  data-id="{{$c3['id']}}"  data-attrs="{{$c3['attrs']}}" data-pid="{{$c3['pid']}}" data-cat1="{{$c3['first_id']}}" data-pic="{{$c3['pic']}}" data-name="{{$c3['name']}}"  data-cat-sn="{{$c3['cat_sn']}}" data-en-name="{{$c3['en_name']}}" data-sort="{{$c3['sort']}}"  data-initial="{{$c3['initial']}}"  data-toggle="modal" data-target="#editModal"style="float:right;position: relative;top:-34px;">编辑</a>
                                                        @if($c3['is_index_recommend']==1)
                                                        <a class="btn-xs btn-primary recommend" data-id="{{$c3['id']}}" data-name="{{$c3['name']}}"
                                                           data-rec="{{$c3['is_index_recommend']}}" style="float:right;position: relative;top:-34px;">取消推荐</a>
                                                        @else
                                                            <a class="btn-xs btn-primary recommend" data-id="{{$c3['id']}}" data-name="{{$c3['name']}}"
                                                               data-rec="{{$c3['is_index_recommend']}}" style="float:right;position: relative;top:-34px;">推荐</a>
                                                        @endif
                                                        
                                                          @if($c3['show_status']==1)
                                            <a class="btn-xs btn-primary show_status" data-id="{{$c3['id']}}" data-name="{{$c3['name']}}"
                                              data-status="{{$c3['show_status']}}" style="float:right;position: relative;top:-34px;">隐藏</a>
                                        @else
                                            <a class="btn-xs btn-primary show_status" data-id="{{$c3['id']}}" data-name="{{$c3['name']}}"
                                               data-status="{{$c3['show_status']}}" style="float:right;position: relative;top:-34px;">显示</a>
                                        @endif
                                                    </td>
                                                </li>
                                                 @endforeach
                                                  @endif     
                                            </ol>
                                        </li>
                                           @endforeach
                               @endif     
                                    </ol>
                                </li> 
                               @endforeach
                               @endif  
                            </ol>
                        </div>
                       <div class="row">
                             
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="ibox ">
                    <div class="dd" id="nestable2">
                    </div>
                </div>
            </div> 
        </div>
    </div>
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content" style="width: 1000px;margin-left: -200px;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-hidden="true">×
                </button>
                <h4 class="modal-title" id="addModalLabel">
                    创建类目
                </h4>
            </div>
            <form class="form-horizontal m-t" id="addForm">
             <div class="modal-body" style="padding: 10px 38px 10px 30px;">
                <div class="form-group">
                   <label class="col-sm-2 control-label" style="margin-right: 15px;">一级类目：</label>
                    <div class="input-group col-sm-8">
                        <select id="first" name="first" data-placeholder="Root" class="chosen-select form-control" style="width:350px;" tabindex="2">
                        <option value="0">选择一级分类</option>
                            @if(!empty($cate1))
                                 @foreach ($cate1 as $c1)
                                <option value="{{$c1['id']}}" hassubinfo="true">{{$c1['name']}}</option>
                            @endforeach
                            @endif  
                        </select>
                        <input type="hidden" value="{{$cate2_json}}" id="cate2_json" />
                    </div>
                </div>
                <div class="form-group">
                   <label class="col-sm-2 control-label" style="margin-right: 15px;">二级类目：</label>
                    <div class="input-group col-sm-8">
                        <select id="second" name="second" data-placeholder="Root" class="chosen-select form-control" style="width:350px;" tabindex="2">
                             
                        </select>
                    </div>
                </div>
                <div class="form-group">
                   <label class="col-sm-2 control-label">分类名称：</label>
                    <div class="col-sm-6">
                        <input id="add_name" name="add_name" class="form-control" type="text" placeholder="分类名称">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">分类编码：</label>
                    <div class="col-sm-6">
                        <input id="add_cat_sn" name="add_cat_sn" class="form-control" type="text" placeholder="分类编码">
                    </div>
                </div>
                <div class="form-group">
                   <label class="col-sm-2 control-label">分类别名：</label>
                    <div class="col-sm-6">
                        <input id="add_en_name" name="add_en_name" class="form-control" type="text" placeholder="分类别名">
                    </div>
                </div>
                <div class="form-group">
                   <label class="col-sm-2 control-label">首字母：</label>
                    <div class="col-sm-2">
                        <input id="add_initial" name="add_initial" class="form-control" type="text" placeholder="" value="">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">排序：</label>
                    <div class="col-sm-2">
                        <input id="add_sort" name="add_sort" class="form-control" type="text" placeholder="0" value="">
                    </div>
                </div>
                <div class="form-group">
                     <label class="col-sm-2 control-label">ICO：</label>
                    <div class="col-sm-8">
                        <!-- <img id="labelimg" src="{{asset('img/p1.jpg')}}"> -->
                        <img id="imgContent" style="width: 80px;height: 80px;">
                        <div class="btn-group">
                            <label title="上传图片" for="inputImage" class="btn btn-primary">
                                <input type="file" accept="image/*" name="inputImage" id="inputImage" class="hide" multiple="false"> 上传图片
                                <input type="hidden" value="" id="add_pic" name="add_pic" />
                            </label>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                   <label class="col-sm-2 control-label">属性：</label>
                   <div class="box" style="width:800px;float:right;">
                        @if(!empty($attrs))
                                 @foreach ($attrs as $attr)
                            <div class="checkbox checkbox-inline">
                                
                                <input type="checkbox" id="inlineCheckbox_{{$attr['id']}}"  value="{{$attr['id']}}" style="margin-top:1px;" class="add_attrs" name="add_attrs">
                                <label for="inlineCheckbox" style="padding-left: 0;">@if($attr['is_sku']==1)<span style="color:#f00;">{{$attr['name']}}</span>@else<span style="color:#1ab394;">{{$attr['name']}}</span>@endif</label>
                             </div>
                        
                      @endforeach
                            @endif  
                   </div>
                </div>
             </div>
             <div class="form-group">
                   <label class="col-sm-2 control-label"></label>
                    <div >
                   <div>提示：<span style="color:#f00;">红的销售属性 </span><span style="color:#1ab394;">绿的商品属性  </span>
                   </div>
                   </div>
            </div>
            
            <div class="modal-footer">
                <button type="submit" class="btn btn-primary" id="add_submit">
                    确定
                </button>
                <button type="button" class="btn btn-default"
                        data-dismiss="modal" >取消
                </button>
            </div>
            </form>
        </div>
    </div>
</div>
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content" style="width: 1000px;margin-left: -200px;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-hidden="true">×
                </button>
                <h4 class="modal-title" id="editModalLabel">
                    修改类目
                </h4>
            </div>
            <form class="form-horizontal m-t" id="editForm">
             <div class="modal-body" style="padding: 10px 38px 10px 30px;">
                <div class="form-group" id="editfirstcat">
                   <label class="col-sm-2 control-label" style="margin-right: 15px;">一级类目：</label>
                    <div class="input-group col-sm-8">
                        <select id="editfirst" name="editfirst" data-placeholder="Root" class="chosen-select form-control" style="width:350px;" tabindex="2">
                        <option value="0">选择一级分类</option>
                            @if(!empty($cate1))
                                 @foreach ($cate1 as $c1)
                                <option value="{{$c1['id']}}" hassubinfo="true">{{$c1['name']}}</option>
                            @endforeach
                            @endif  
                        </select>
                        <input type="hidden" value="{{$cate2_json}}" id="cate2_json" />
                    </div>
                </div>
                <div class="form-group" id="editseccat">
                   <label class="col-sm-2 control-label" style="margin-right: 15px;">二级类目：</label>
                    <div class="input-group col-sm-8">
                        <select id="editsecond" name="editsecond" data-placeholder="Root" class="chosen-select form-control" style="width:350px;" tabindex="2">
                             
                        </select>
                    </div>
                </div>
                <div class="form-group">
                   <label class="col-sm-2 control-label">分类名称：</label>
                    <div class="col-sm-6">
                        <input id="edit_name" name="edit_name" class="form-control" type="text" placeholder="分类名称">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">分类编码：</label>
                    <div class="col-sm-6">
                        <input id="edit_cat_sn" name="edit_cat_sn" class="form-control" type="text" placeholder="分类编码">
                    </div>
                </div>
                <div class="form-group">
                   <label class="col-sm-2 control-label">分类别名：</label>
                    <div class="col-sm-6">
                        <input id="edit_en_name" name="edit_en_name" class="form-control" type="text" placeholder="分类别名">
                    </div>
                </div>
                <div class="form-group">
                   <label class="col-sm-2 control-label">首字母：</label>
                    <div class="col-sm-2">
                        <input id="edit_initial" name="edit_initial" class="form-control" type="text" placeholder="" value="">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">排序：</label>
                    <div class="col-sm-2">
                        <input id="edit_sort" name="edit_sort" class="form-control" type="text" placeholder="0" value="">
                    </div>
                </div>
                <div class="form-group">
                     <label class="col-sm-2 control-label">ICO：</label>
                    <div class="col-sm-8">
                        <img id="editImgContent" style="width: 80px;height: 80px;"  >
                        <div class="btn-group">
                            <label title="上传图片" for="editInputImage" class="btn btn-primary">
                                <input type="file" accept="image/*" name="editInputImage" id="editInputImage" class="hide" multiple="false"> 上传图片
                                <input type="hidden" value="" id="edit_pic" name="edit_pic" />
                            </label>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                   <label class="col-sm-2 control-label">属性：</label>
                   <div class="box" style="width:800px;float:right;">
                        @if(!empty($attrs))
                                     @foreach ($attrs as $attr)
                        <div class="checkbox checkbox-inline">
                            
                            <input type="checkbox" id="inlineCheckbox_{{$attr['id']}}"  value="{{$attr['id']}}" style="margin-top:1px;" class="edit_attrs" name="edit_attrs">
                            <label for="inlineCheckbox" style="padding-left: 0;">@if($attr['is_sku']==1)<span style="color:#f00;">{{$attr['name']}}</span>@else<span style="color:#1ab394;">{{$attr['name']}}</span>@endif</label>
                         </div>
                            
                          @endforeach
                                @endif
                   </div>     

                </div>
            </div>
                  <div class="form-group">
                   <label class="col-sm-2 control-label"></label>
                    <div >
                   <div>提示：<span style="color:#f00;">红的销售属性 </span><span style="color:#1ab394;">绿的商品属性  </span>
                   </div>
                   </div>
            </div>
            <div class="modal-footer">
                <button type="submit" class="btn btn-primary" id="edit_submit">
                    确定
                </button>
                <button type="button" class="btn btn-default"
                        data-dismiss="modal" >取消
                </button>
            </div>
            </form>
        </div>
    </div>
</div>
    <!-- Nestable List -->
    <script src="{{asset('js/plugins/nestable/jquery.nestable.js')}}"></script>
    <script src="{{asset('js/plugins/validate/jquery.validate.min.js')}}"></script>
    <script src="{{asset('js/plugins/validate/messages_zh.min.js')}}"></script>
    <script src="{{asset('js/plugins/layer/layer.min.js')}}"></script>
   <script>
        $(document).ready(function () {

        	 //新增上传图片
            function upload(file,id){
                var reader = new FileReader();  
                //创建文件读取相关的变量  
                var imgFile;  
                //为文件读取成功设置事件  
                reader.onload=function(e) {  
                    imgFile = e.target.result; 
                    $("#"+id).attr('src', imgFile);  
                };  
                //正式读取文件  
                reader.readAsDataURL(file);
            }
            $('#inputImage').change(function(img){
                upload(this.files[0],"imgContent");
                var files =  this.files[0];
                var formFile = new FormData();   
                formFile.append("image", files);
                $.ajax({
                     url: "/uploads/images",
                    type: 'post',
                    data: formFile,
                    dataType:"json",
                    contentType: false,  
                    processData: false, 
                    success: function(data) {
                       $("#add_pic").val(data.path);
                    },
                    error: function(data) {
                       console.log("error");
                    }
                });
            })
            
             $('#editInputImage').change(function(img){
                upload(this.files[0],"editImgContent");
                var files =  this.files[0];
                var formFile = new FormData();   
                formFile.append("image", files);
                $.ajax({
                     url: "/uploads/images",
                    type: 'post',
                    data: formFile,
                    dataType:"json",
                    contentType: false,  
                    processData: false, 
                    success: function(data) {
                       $("#edit_pic").val(data.path);
                    },
                    error: function(data) {
                       console.log("error");
                    }
                });
            })
            
            
            var updateOutput = function (e) {
                var list = e.length ? e : $(e.target),
                    output = list.data('output');
                if (window.JSON) {
                    output.val(window.JSON.stringify(list.nestable('serialize'))); //, null, 2));
                } else {
                    output.val('浏览器不支持');
                }
            };
            // activate Nestable for list 1
            $('#nestable').nestable({
                group: 1
            }).on('change', updateOutput);

            // activate Nestable for list 2
            $('#nestable2').nestable({
                group: 1
            }).on('change', updateOutput);

            // output initial serialised data
            updateOutput($('#nestable').data('output', $('#nestable-output')));
            updateOutput($('#nestable2').data('output', $('#nestable2-output')));

            $('#nestable-menu').on('click', function (e) {
                var target = $(e.target),
                    action = target.data('action');
                if (action === 'expand-all') {
                    $('.dd').nestable('expandAll');
                }
                if (action === 'collapse-all') {
                    $('.dd').nestable('collapseAll');
                }
            });
            var edit_id = 0;//修改属性的id
            // validate signup form on keyup and submit
            var icon = "<i class='fa fa-times-circle'></i> ";
            //新增
            $("#addForm").validate({
                rules: {
                	add_name: "required",
                },
                messages: {
                	add_name: icon + "请输入名称",
                },
                submitHandler:function(form){
                    var add_name = $('#add_name').val();
                    var add_initial= $('#add_initial').val();
                    var add_pic= $('#add_pic').val();
                    var add_cat_sn= $('#add_cat_sn').val();
                    var add_en_name= $('#add_en_name').val();
                    var add_sort= $('#add_sort').val();
                    var first_cat = $('#first').val();
                    var second_cat = $('#second').val();
                    var pid = 0;
                    var root_id = 1;
                    var level_type = "first_level";
                    var first_id=first_cat;

                    var attrs=new Array();
                    $('input[name="add_attrs"]:checked').each(function(){
                    	attrs.push($(this).val()); 
                    });
					if(add_initial.length > 1){
						   layer.alert("首字母只能一个字符", {
                               icon: 2
                           });
                           return false;
					}
                    
                    if(first_cat == 0 ){
                        pid=0;
                        root_id = 1;
                    }else{
                    	 if(second_cat == 0 ){
                    		pid=first_cat;
                            root_id = 2;
                            level_type = "second_level";
                            
                    	 }else{
                    		level_type = "third_level";
                    		pid=second_cat;
                     		root_id = 3;
                    	 }
                    }
                    
                    $.ajax({
                    	url : '{{Request::url()}}/add',
                        type : 'post',
                        dataType : "json",
                        data : {name:add_name,initial:add_initial,pid:pid,pic:add_pic,cat_sn:add_cat_sn,en_name:add_en_name,sort:add_sort,first_id:first_id,root_id:root_id,level_type:level_type,attrs:attrs},
                        success : function(data) {
                            if (0 == data.code) {
                                layer.alert("添加成功", {
                                    icon: 1
                                }, function () {
                                    location.reload();
                                });
                            } else {
                                layer.alert(data.message, {
                                    icon: 2
                                });
                            }
                        }
                    });
                }
            });

    

            $('#editModal').on('show.bs.modal', function (e) {
                edit_id = $(e.relatedTarget).attr("data-id");//编辑时属性的id
                edit_name = $(e.relatedTarget).attr("data-name"); 
                edit_pic = $(e.relatedTarget).attr("data-pic"); 
                edit_cat_sn = $(e.relatedTarget).attr("data-cat-sn"); 
                edit_en_name = $(e.relatedTarget).attr("data-en-name"); 
                edit_initial= $(e.relatedTarget).attr("data-initial"); 
                edit_sort = $(e.relatedTarget).attr("data-sort"); 
                edit_cat1=$(e.relatedTarget).attr("data-cat1");
                edit_pid=$(e.relatedTarget).attr("data-pid"); 
                edit_attrs = $(e.relatedTarget).attr("data-attrs"); 

   			 	if(edit_attrs ){
   			 	   var attrs_obj = eval("("+edit_attrs+")");

   	               
   				$(".edit_attrs").prop("checked",false);
                   $(".edit_attrs").each(function(){
                   	if( $(this).val()  in attrs_obj){
                       	$(this).prop("checked","checked");
                   	} 
                   });
   			 	}

   			 $("#editfirst").val(0);
         	$('#editsecond').val(0);

                if(edit_pid != 0 ){
                	$("#editfirst").val(edit_cat1);
                	$("#editfirst").trigger("change");
                	$('#editsecond').val(edit_pid);
                }

                var second_cat = $('#editsecond').val();
                if(second_cat>0){
                	$('#editseccat').show();
                }else{
                	$('#editseccat').hide();
                } 

                var first_cat = $('#editfirst').val();
                if(first_cat>0){
                	$('#editfirstcat').show();
                }else{
                	$('#editfirstcat').hide();
                } 
                $('#edit_name').val(edit_name);
                $('#edit_pic').val(edit_pic);
                $('#edit_cat_sn').val(edit_cat_sn);
                $('#edit_en_name').val(edit_en_name);
                $('#edit_initial').val(edit_initial);
                $('#edit_sort').val(edit_sort);
                $('#editImgContent').attr('src',edit_pic);
                
            })
            //编辑
            $("#editForm").validate({
                rules: {
                	edit_name: "required",
                },
                messages: {
                	edit_name: icon + "请输入名称",
                },
                submitHandler:function(form){
                    

                    var attrs=new Array();
                    $('input[name="edit_attrs"]:checked').each(function(){
                    	attrs.push($(this).val()); 
                    });


                    var edit_name = $('#edit_name').val();
                    var edit_pic = $('#edit_pic').val();
                    var edit_cat_sn = $('#edit_cat_sn').val();
                    var edit_en_name = $('#edit_en_name').val();
                    var edit_initial = $('#edit_initial').val();
                    var edit_sort = $('#edit_sort').val();
                    var edit_pic = $('#edit_pic').val();

                  
                    var first_cat = $('#editfirst').val();
                    var second_cat = $('#editsecond').val();
                    var pid = 0;
                    var root_id = 1;
                    var level_type = "first_level";
                    var first_id=first_cat;


                	if(edit_initial.length > 1){
						   layer.alert("首字母只能一个字符", {
                            icon: 2
                        });
                        return false;
					}
					
                 
                    if(first_cat == 0 ){
                        pid=0;
                        root_id = 1;
                    }else{
                         //custom s 暂时修改 2018年8月6日19:16:50 CuiLiwu 解决修改二级分类变成三级分类的 BUG.
                    	 if(second_cat == 0 || second_cat==null){
                    		pid=first_cat;
                            root_id = 2;
                            level_type = "second_level";
                            
                    	 }else{
                    		level_type = "third_level";
                    		pid=second_cat;
                     		root_id = 3;
                    	 }
                    }
                    
                    $.ajax({
                    	url : '{{Request::url()}}/'+edit_id+"/edit",
                        type : 'PUT',
                        dataType : "json",
                        data : {name:edit_name,initial:edit_initial,pid:pid,pic:edit_pic,cat_sn:edit_cat_sn,en_name:edit_en_name,sort:edit_sort,first_id:first_id,root_id:root_id,level_type:level_type,attrs:attrs},
                     	success : function(data) {
                            if (0 == data.code) {
                                layer.alert("修改成功", {
                                    icon: 1
                                }, function () {
                                    location.reload();
                                });
                            } else {
                                layer.alert(data.message, {
                                    icon: 2
                                });
                            }
                        }
                    });
                }
            });
            //删除
            $('.del').click(function(){
                var id = $(this).attr('data-id');
                layer.confirm('是否删除？', {
                    btn: ['确认', '取消'] //可以无限个按钮
                }, function (index) {
                    $.ajax({
                    	url: '{{Request::url()}}/'+id+"/delete",
                        type: 'DELETE',
                        dataType: 'json',
                        data: {'id': id},
                        success: function (data) {
                            if (0 == data.code) {
                                layer.alert("删除成功", {
                                    icon: 1
                                }, function () {
                                    location.reload();
                                });
                            } else {
                                layer.alert(data.message, {
                                    icon: 2
                                });
                            }
                        }
                    });
                }, function (index, layero) {
                    layer.close(index);
                });
            })
            //保存
            $('.sev').click(function(){
                console.log($('.del'))
                var id = $(this).attr('data-id');
                layer.confirm('是否保存？', {
                    btn: ['确认', '取消'] //可以无限个按钮
                }, function (index) {
                    $.ajax({
                        url: '',
                        type: 'GET',
                        dataType: 'json',
                        data: {'id': id},
                        success: function (data) {
                            if (0 == data.code) {
                                layer.alert(data.message, {
                                    icon: 1
                                }, function () {
                                    location.reload();
                                });
                            } else {
                                layer.alert(data.message, {
                                    icon: 2
                                });
                            }
                        }
                    });
                }, function (index, layero) {
                    layer.close(index);
                });
            })

            //推荐类目
            $('.recommend').click(function(){
                var id = $(this).attr('data-id');
                var rec = $(this).attr('data-rec');
                var cname ='<span class="text-danger">'+$(this).attr('data-name')+'</span>';
                var flag = rec==0?'设置':'取消';
                var _this = $(this);
                // 修改完成后要填入得字符串
                var strContent = rec==0?'取消推荐':'推荐';
                var statusNew = rec==0?1:0;

                layer.confirm('是否'+flag+'类目【 '+cname+' 】为首页推荐类目？', {
                    btn: ['确认', '取消'] //可以无限个按钮
                }, function (index) {
                    $.ajax({
                        url: '{{Request::url()}}/'+id+"/recommend",
                        type: 'POST',
                        dataType: 'json',
                        data: {'id': id,'rec':rec},
                        success: function (data) {
                            if (0 == data.code) {
                                layer.alert(flag+"成功", {
                                    icon: 1
                                }, function (index) {
                                    _this.attr('data-rec',statusNew);
                                    _this.html(strContent);
                                    console.log(_this);
                                    layer.close(index);
                                });
                            } else {
                                layer.alert(data.message, {
                                    icon: 2
                                });
                            }
                        }
                    });
                }, function (index, layero) {
                    layer.close(index);
                });
            });

            //商城首页类目推荐
            $('.shopindex_recommend').click(function(){
                var id = $(this).attr('data-id');
                var rec = $(this).attr('data-rec');
                var cname ='<span class="text-danger">'+$(this).attr('data-name')+'</span>';
                var flag = rec==0?'设置':'取消';
                var _this = $(this);
                // 修改完成后要填入得字符串
                var strContent = rec==0?'取消商城推荐':'商城推荐';
                var statusNew = rec==0?1:0;

                layer.confirm('是否'+flag+'类目【 '+cname+' 】为商城首页安防市场推荐类目？', {
                    btn: ['确认', '取消'] //可以无限个按钮
                }, function (index) {
                    $.ajax({
                        url: '{{Request::url()}}/'+id+"/shopindex_recommend",
                        type: 'POST',
                        dataType: 'json',
                        data: {'id': id,'rec':rec},
                        success: function (data) {
                            if (0 == data.code) {
                                layer.alert(flag+"成功", {
                                    icon: 1
                                }, function (index) {
                                    _this.attr('data-rec',statusNew);
                                    _this.html(strContent);
                                    console.log(_this);
                                    layer.close(index);
                                });
                            } else {
                                layer.alert(data.message, {
                                    icon: 2
                                });
                            }
                        }
                    });
                }, function (index, layero) {
                    layer.close(index);
                });
            })
            
            //显示隐藏类目
            $('.show_status').click(function(){
                var id = $(this).attr('data-id');
                var status = $(this).attr('data-status');
                var cname ='<span class="text-danger">'+$(this).attr('data-name')+'</span>';
                var flag = status==0?'显示':'隐藏';
                var _this = $(this);
                // 修改完成后要填入得字符串
                var strContent = status==0?'隐藏':'显示';
                var statusNew = status==0?1:0;

                layer.confirm('是否'+flag+'类目？', {
                    btn: ['确认', '取消'] //可以无限个按钮
                }, function (index) {
                    $.ajax({
                        url: '{{Request::url()}}/'+id+"/showstatus",
                        type: 'POST',
                        dataType: 'json',
                        data: {'id': id,'status':status},
                        success: function (data) {
                            if (0 == data.code) {
                                layer.alert(flag+"成功", {
                                    icon: 1
                                }, function (index) {
                                    _this.attr('data-status',statusNew);
                                    _this.html(strContent);
                                    console.log(_this);
                                    layer.close(index);
                                });
                            } else {
                                layer.alert(data.message, {
                                    icon: 2
                                });
                            }
                        }
                    });
                }, function (index, layero) {
                    layer.close(index);
                });
            })
            $("#first").change(function(){
                var cate2_json = eval("("+$("#cate2_json").val()+")");
                var html = "<option value='0'>请选择二级分类</option>";
                var cates = cate2_json[$(this).val()];
                for(cate in cates){
                	html +="<option value='"+cates[cate]['id']+"' >"+cates[cate]['name']+"</option>";
                }
                $("#second").html(html);
            });

            $("#editfirst").change(function(){
                var cate2_json = eval("("+$("#cate2_json").val()+")");
                var html = "<option value='0'>请选择二级分类</option>";
                var cates = cate2_json[$(this).val()];
                for(cate in cates){
                	html +="<option value='"+cates[cate]['id']+"' >"+cates[cate]['name']+"</option>";
                }
                $("#editsecond").html(html);
            });
        });
    </script>
@endsection
